package com.kk.service.impl;

import com.kk.dao.IUserDao;
import com.kk.model.Menu;
import com.kk.model.User;
import com.kk.repository.RoleMenuRelRepository;
import com.kk.repository.UserRepository;
import com.kk.service.MenuService;
import com.kk.service.UserService;
import com.kk.util.JSONUtil;
import com.kk.util.redis.RedisUtils;
import com.kk.util.request.Reqbody;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Log4j2
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private IUserDao userDao;

    @Autowired
    private RoleMenuRelRepository roleMenuRelRepository;

    @Autowired
    private MenuService menuService;
    @Autowired
    private UserRepository userRepository;

    public User findUserByName(String name) {
        return userDao.findUserByName(name);
    }

    /**
     * 获取用户登录后返回的数据
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> getLoginUserAndMenuInfo(User user) {
        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        List<Menu> allMenuList = menuService.getAllMenuList(user);
        List<Menu> topMenuList = menuService.getTopMenuList(user);
        List<Menu> menuTreeList = menuService.getMenuTreeList(topMenuList, allMenuList);
        List<Menu> menuButtonList = menuService.getButtonList(user);
        result.put("menuList", menuTreeList);
        result.put("buttonList", menuButtonList);
        return result;
    }

    @Override
    public List<Menu> getMenuListByUser(User user) {
        List<Menu> allMenuList = menuService.getAllMenuList(user);
        List<Menu> topMenuList = menuService.getTopMenuList(user);
        List<Menu> menuTreeList = menuService.getMenuTreeList(topMenuList, allMenuList);
        return menuTreeList;
    }

    @Override
    public List<User> getAllUser() {
        return userRepository.findAll(Sort.by(Sort.Order.asc("sort")));
    }

    @Override
    public List<User> findByUsernameLike(String username) {

        return userRepository.findByUsernameLike(username);
    }

    @Override
    public Page<User> findUserNoCriteria(Integer page, Integer size) {
        if (page > 1) {
            page = page - 1;

        } else {
            page = 0;
        }
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.ASC, "sort");
        return userRepository.findAll(pageable);
    }

    @Override
    public Page<User> findUserByCondition(Reqbody reqbody) {
        System.out.println(reqbody.getExample());
        int page = reqbody.getPageIndex();
        if (page > 1) {
            page = page - 1;

        } else {
            page = 0;
        }
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("createTime", ExampleMatcher.GenericPropertyMatchers.contains())
//                .withMatcher("sort", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("lastModifiedTime", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("sort");
        User user = JSONUtil.parse(reqbody.getExample(), User.class);
        Example<User> ex = Example.of(user, matcher);
        Pageable pageable = PageRequest.of(page, reqbody.getPageSize());
        Page<User> result = userRepository.findAll(ex, pageable);
        return result;
    }

//    //暂时只去检验redis中是否有此token，减轻服务查询数据库压力
//    @Override
//    public boolean authToken(String token) {
//        boolean isExist = false;
//        if (redisUtils != null) {
//            isExist = redisUtils.exists(token);
//            return isExist;
//        }else{
//            log.error("redis为空");
//            return isExist;
//        }
//    }
}
